﻿using HalconDotNet;
using LD.Host;
using LD.Tool;
using LD.Vision;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 右下角弹窗.DiagShow;

namespace LD.Tool
{
    public partial class ToolTemplateMatchHightFrm : FrmVisionToolBase
    {
        private HImage img;
        private string imgBind;
        private FrmTemplate frmTemplate;
        public ToolTemplateMatchHightFrm()
        {
            InitializeComponent();
            this.Shown += ToolTemplateMatchHightFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
            Init();
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CameraName))
            {
                Dialog dialog = new Dialog();
                dialog.Error("相机不能为空");
                return;
            }

            RoiPath();

            if (frmTemplate.TemplateModel != null)
            {
                var param = new ToolTemplateMatchParam()
                {
                    ImageSource = this.txt_imagePath.Text,
                    TemplateModel = frmTemplate.TemplateModel,
                    CameraName = this.CameraName
                };
                TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);

                frmTemplate.SaveRoi();
            }
        }

        private void ToolTemplateMatchHightFrm_Shown(object sender, EventArgs e)
        {
            InitParam();
        }

        private void Init()
        {
            InitUI();
        }
        private void InitUI()
        {
            frmTemplate = new FrmTemplate();
            frmTemplate.Dock = DockStyle.Fill;
            frmTemplate.TopLevel = false;
            frmTemplate.FormBorderStyle = FormBorderStyle.None;
            frmTemplate.Show();
            this.panel4.Controls.Add(frmTemplate);

            if (!string.IsNullOrEmpty(CameraName))
            {
                RoiPath();
            }
        }
        private void RoiPath()
        {
            if (!Directory.Exists(Path.Combine(Appbus.BasePath, CameraName + "\\Template")))
                Directory.CreateDirectory(Path.Combine(Appbus.BasePath, "Vision\\" + CameraName + "\\Template"));
            //roi目录
            frmTemplate.TemplateModel.RoiTmpXmlPath = Path.Combine(Appbus.BasePath, "Vision\\" + CameraName + "\\Template\\RoiTmp.xml");
            frmTemplate.TemplateModel.RoiXmlPath = Path.Combine(Appbus.BasePath, "Vision\\" + CameraName + "\\Template\\Roi.xml");
        }
        private void InitParam()
        {
            if (TB_Tool.Param == null) return;
            var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ToolTemplateMatchParam>(TB_Tool.Param);
            if (param != null)
            {
                this.CameraName = param.CameraName;
                txt_imagePath.Text = param.ImageSource;
                frmTemplate.HImage = (HImage)base.GetToolValue(TB_Tool, param.ImageSource);
                frmTemplate.TemplateModel = param.TemplateModel;
            }
        }
        private void btn_dir_Click(object sender, EventArgs e)
        {
            //弹窗
            using (var ofd = new OpenFileDialog())
            {
                ofd.Title = "打开数据";
                //ofd.Filter = "(*.bmp)|*.bmp|(*.jpg) | *.jpg";
                ofd.Filter = "(*.ldc)|*.ldc;";
                ofd.RestoreDirectory = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string fileName = ofd.FileName;
                    TB_Tool.Param = fileName;
                }
            }
        }

        private void btn_bind_Click(object sender, EventArgs e)
        {
            FrmOutputBind bindFrm = new FrmOutputBind(TB_Tool.FlowName, typeof(HImage));
            if (bindFrm.ShowDialog() == DialogResult.OK)
            {
                var bind = bindFrm.BindOutput;
                this.txt_imagePath.Text = bind;
                this.frmTemplate.HImage = (HImage)base.GetToolValue(TB_Tool, bind);

            }
        }
    }
}
